package com.zhima;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;
import com.zhima.config.MybatisEmcProperties;

import java.util.Collections;
import java.util.Map;

public class MybatisEmcGenerator {
    private final MybatisEmcProperties mybatisEmcProperties;

    public MybatisEmcGenerator(MybatisEmcProperties mybatisEmcProperties) {
        this.mybatisEmcProperties = mybatisEmcProperties;
    }

    public void generateCode() {
        MybatisEmcProperties.PackageInfo packageInfo = mybatisEmcProperties.getPackageInfo();
        String outPutRootPath = String.format("%s/%s/generate/src/main", mybatisEmcProperties.getGenerateBaseDirPath(), packageInfo.getParentModulePackageName());
        FastAutoGenerator.create(mybatisEmcProperties.getUrl(), mybatisEmcProperties.getUsername(), mybatisEmcProperties.getPassword())
                // 全局配置
                .globalConfig(builder -> {
                    builder.author(mybatisEmcProperties.getAuthor())   // 作者
                            .fileOverride()    // 覆盖已生成文件
                            .outputDir(String.format("%s/java", outPutRootPath))     // 指定输出目录
                            .enableSwagger()   // 开启 swagger 的 api
                            .disableOpenDir()  // 禁止打开输出目录
                            .dateType(DateType.ONLY_DATE) // 时间策略
                            .commentDate("yyyy-MM-dd") //注释日期
                            .build();
                })
                // 包配置
                .packageConfig(builder -> {
                    builder.parent(packageInfo.getParentPackageName())   // 父包名
                            .moduleName("")                 // 父包模块名
                            .entity(packageInfo.getEntityPackageName())               // Entity 包名
                            .service(packageInfo.getServicePackageName())             // service 包名
                            .serviceImpl(packageInfo.getServiceImplPackageName())    //
                            .mapper(packageInfo.getMapperPackageName())               //
                            .xml(packageInfo.getMapperXmlPackageName())              // MapperXML 包名
                            .controller(packageInfo.getControllerPackageName())       //
                            .pathInfo(Collections.singletonMap(OutputFile.xml,
                                    String.format("%s/resources/%s", outPutRootPath, packageInfo.getMapperXmlPackageName()))
                            ) // 自定义某个文件的路径
                            .build();
                })
                // 策略配置
                .strategyConfig(builder -> {
                    builder.addTablePrefix("%")
                            .entityBuilder()  // 实体类配置
                            .enableTableFieldAnnotation()
                            .idType(IdType.AUTO)
                            .controllerBuilder() // controller 接口配置
                            .enableHyphenStyle() // 开启驼峰转连字符
                            .enableRestStyle()
                            .serviceBuilder() // service 配置
                            .formatServiceFileName("%sService")
                            .formatServiceImplFileName("%sServiceImpl")
                            .mapperBuilder()  //mapper 配置
                            .superClass(BaseMapper.class)
                            .enableMapperAnnotation()
                            .enableBaseResultMap()
                            .enableBaseColumnList()
                            .formatMapperFileName("%sMapper")
                            .formatXmlFileName("%sMapper")
                            .build();
                })
                // 模板配置
                .templateEngine(new VelocityTemplateEngine()) // 默认
                .templateConfig(builder -> builder.controller("/templates/controller.java") // 自定义 controller 模板
                        .build())
                .execute();
    }
}

